package com.example.hzq.food.fragment;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import com.example.hzq.food.R;
import com.example.hzq.food.activity.Buydetail;
import com.example.hzq.food.adapter.ShopFoodadapter;
import com.example.hzq.food.bean.BuyBean;
import com.example.hzq.food.bean.FoodBean;
import com.example.hzq.food.model.Model;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static com.example.hzq.food.activity.Buydetail.paidflag;
import static com.example.hzq.food.adapter.Shopadapter.shop_id;

/**
 * Created by hzq on 2017/6/15.
 */

public class ShopFoodfragment extends Fragment{
    private RecyclerView recyclerView;
    private  RecyclerView.LayoutManager layoutManager;
    private ShopFoodadapter shopFoodadapter;
    private List<FoodBean>list=null;
    private Context context;
    private BuyBean ent,back;
    private TextView foodnum,foodsum;
    private Button tobuy;
    private View view;
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState){
        view=inflater.inflate(R.layout.shopfoodfragment,container,false);
        context=getActivity();
        foodnum=(TextView)view.findViewById(R.id.foodnum);
        foodsum=(TextView)view.findViewById(R.id.foodsum);
        tobuy=(Button)view.findViewById(R.id.tobuy);
        recyclerView=(RecyclerView)view.findViewById(R.id.recyclerview2);
        layoutManager=new LinearLayoutManager(context);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setHasFixedSize(true);
        shopFoodadapter=new ShopFoodadapter(context);
        shopFoodadapter.setList(list);
        recyclerView.setAdapter(shopFoodadapter);
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                super.onScrollStateChanged(recyclerView, newState);
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
            }
        });
        Model model=new Model();
        Call<List<FoodBean>>call=model.getfood(shop_id);
        call.enqueue(new Callback<List<FoodBean>>() {
            @Override
            public void onResponse(Call<List<FoodBean>> call, Response<List<FoodBean>> response) {
                list=response.body();
                shopFoodadapter.setList(list);
            }

            @Override
            public void onFailure(Call<List<FoodBean>> call, Throwable t) {

            }
        });
        tobuy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent =new Intent(context,Buydetail.class);
                Bundle bundle=new Bundle();
                bundle.putSerializable("bean",ent);
                intent.putExtras(bundle);
                startActivity(intent);
            }
        });
        return view;
    }
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(BuyBean event) {
        if (event != null)
            changeStatus(event);
    }

    private void changeStatus(BuyBean event) {
        if (event.getFoodnum() > 0) {
            ent=event;
            foodnum.setText(event.getFoodnum()+"");
            foodnum.setVisibility(view.VISIBLE);
            foodsum.setText("¥" + String.valueOf(ent.getFoodsum()));
            tobuy.setClickable(true);
            tobuy.setBackgroundColor(getResources().getColor(R.color.tuBuyBgGreen));
            tobuy.setTextColor(getResources().getColor(R.color.white));
        } else {
            foodnum.setText(null);
            foodnum.setVisibility(view.INVISIBLE);
            foodsum.setText("购物车为空");
            foodsum.setTextColor(R.color.white);
            tobuy.setClickable(false);
            tobuy.setBackgroundColor(getResources().getColor(R.color.tuBuyBg));
            tobuy.setTextColor(getResources().getColor(R.color.tuBuyText));
        }
    }

    @Override
    public void onStart() {
        super.onStart();
        EventBus.getDefault().register(this);
    }
    @Override
    public void onStop() {
        super.onStop();
        EventBus.getDefault().unregister(this);
    }

    @Override
    public void onResume() {
        super.onResume();
        if (paidflag){
            shopFoodadapter.buyBean=null;
            shopFoodadapter=new ShopFoodadapter(context);
            shopFoodadapter.setList(list);
            recyclerView.setAdapter(shopFoodadapter);
            foodnum.setText(null);
            foodnum.setVisibility(view.INVISIBLE);
            foodsum.setText("购物车为空");
            foodsum.setTextColor(R.color.white);
            tobuy.setClickable(false);
            tobuy.setBackgroundColor(getResources().getColor(R.color.tuBuyBg));
            tobuy.setTextColor(getResources().getColor(R.color.tuBuyText));
        }
    }
}
